package com.gjy.statemachine.config;

import com.gjy.statemachine.event.OrderStatusChangeEvent;
import com.gjy.statemachine.module.TbOrder;
import com.gjy.statemachine.state.OrderStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.guard.Guard;

import java.math.BigDecimal;
import java.util.EnumSet;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-02 14:32:01
 */
@Configuration
@EnableStateMachine(name = "orderStateMachine")
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderStatus, OrderStatusChangeEvent> {

    /**
     * 配置状态
     */
    public void configure(StateMachineStateConfigurer<OrderStatus, OrderStatusChangeEvent> states) throws Exception {
        states.withStates().initial(OrderStatus.WAIT_PAYMENT).states(EnumSet.allOf(OrderStatus.class));
    }

    /**
     * 配置状态转换事件关系
     */
    public void configure(StateMachineTransitionConfigurer<OrderStatus, OrderStatusChangeEvent> transitions) throws Exception {
        transitions
                //支付事件:待支付-》待发货
                .withExternal().source(OrderStatus.WAIT_PAYMENT).target(OrderStatus.WAIT_DELIVER).event(OrderStatusChangeEvent.PAYED)
                .action(payAction()).guard(payGuard())
                .and()
                //发货事件:待发货-》待收货
                .withExternal().source(OrderStatus.WAIT_DELIVER).target(OrderStatus.WAIT_RECEIVE).event(OrderStatusChangeEvent.DELIVERY)
                .and()
                //收货事件:待收货-》已完成
                .withExternal().source(OrderStatus.WAIT_RECEIVE).target(OrderStatus.FINISH).event(OrderStatusChangeEvent.RECEIVED);
    }

    // 定义动作
    @Bean
    public Action<OrderStatus, OrderStatusChangeEvent> payAction() {
        return new Action<OrderStatus, OrderStatusChangeEvent>() {
            @Override
            public void execute(StateContext<OrderStatus, OrderStatusChangeEvent> context) {
                // 处理支付动作，比如更新订单支付时间、调用支付接口等
                System.out.println("执行支付动作");
                /*TbOrder order = context.getMessage().getHeaders().get("order", TbOrder.class);
                order.setStatus(OrderStatus.WAIT_DELIVER.getKey());*/
                // 这里可以添加具体的业务逻辑
            }
        };
    }

    // 定义守卫条件
    @Bean
    public Guard<OrderStatus, OrderStatusChangeEvent> payGuard() {
        return context -> {
            // 判断订单金额是否大于 0，只有金额大于 0 才能支付
            TbOrder order = context.getMessage().getHeaders().get("order", TbOrder.class);
            return order.getPrice().compareTo(BigDecimal.ZERO) > 0;
        };
    }

}
